package com.ly.heart.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.FileEntity;
import com.ly.heart.service.IFileInfoService;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.ProjectAppointManagerMapper;
import com.ly.heart.domain.ProjectAppointManagerEntity;
import com.ly.heart.service.IProjectAppointManagerService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 委任项目经理记录Service业务层处理
 *
 * @author cxl
 * @date 2024-05-14
 */
@Service
public class ProjectAppointManagerServiceImpl implements IProjectAppointManagerService {

    @Resource
    private ProjectAppointManagerMapper projectAppointManagerMapper;
    @Resource
    private IFileInfoService iFileInfoService;

    /**
     * 查询委任项目经理记录
     *
     * @param id 委任项目经理记录主键
     * @return 委任项目经理记录
     */
    @Override
    public ProjectAppointManagerEntity selectProjectAppointManagerEntityById ( String id ) {
        return projectAppointManagerMapper.selectProjectAppointManagerEntityById ( id );
    }

    /**
     * 查询委任项目经理记录列表
     *
     * @param projectAppointManagerEntity 委任项目经理记录
     * @return 委任项目经理记录
     */
    @Override
    public List<ProjectAppointManagerEntity> selectProjectAppointManagerEntityList ( ProjectAppointManagerEntity projectAppointManagerEntity ) {
        return projectAppointManagerMapper.selectProjectAppointManagerEntityList ( projectAppointManagerEntity );
    }

    /**
     * 新增委任项目经理记录
     * 新增附件列表记录
     *
     * @param projectAppointManagerEntity 委任项目经理记录
     * @return 结果
     */
    @Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public int insertProjectAppointManagerEntity ( ProjectAppointManagerEntity projectAppointManagerEntity ) {
        projectAppointManagerEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        projectAppointManagerEntity.setCreateTime ( DateUtils.getNowDate () );
        //处理附件列表
        if ( ! CollectionUtils.isEmpty ( projectAppointManagerEntity.getFileIds () ) ) {
            List<FileEntity> fileEntities = new ArrayList<> ();
            projectAppointManagerEntity.getFileIds ().forEach ( fileId -> {
                FileEntity fileEntity = new FileEntity ();
                fileEntity.setRelationId ( projectAppointManagerEntity.getId () );
                fileEntity.setId ( fileId );
                fileEntity.setChangeTime ( DateUtils.getNowDate () );
            } );
            boolean b = iFileInfoService.updateBatchById ( fileEntities );
            if ( ! b ) {
                throw new RuntimeException ( "修改委任项目经理附件失败" );
            }
        }
        return projectAppointManagerMapper.insertProjectAppointManagerEntity ( projectAppointManagerEntity );
    }

    /**
     * 修改委任项目经理记录
     *
     * @param projectAppointManagerEntity 委任项目经理记录
     * @return 结果
     */
    @Override
    public int updateProjectAppointManagerEntity ( ProjectAppointManagerEntity projectAppointManagerEntity ) {
        projectAppointManagerEntity.setChangeTime ( DateUtils.getNowDate () );
        return projectAppointManagerMapper.updateProjectAppointManagerEntity ( projectAppointManagerEntity );
    }

    /**
     * 批量删除委任项目经理记录
     *
     * @param ids 需要删除的委任项目经理记录主键
     * @return 结果
     */
    @Override
    public int deleteProjectAppointManagerEntityByIds ( String[] ids ) {
        return projectAppointManagerMapper.deleteProjectAppointManagerEntityByIds ( ids );
    }

    /**
     * 删除委任项目经理记录信息
     *
     * @param id 委任项目经理记录主键
     * @return 结果
     */
    @Override
    public int deleteProjectAppointManagerEntityById ( String id ) {
        return projectAppointManagerMapper.deleteProjectAppointManagerEntityById ( id );
    }
}
